﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace MyMovieSystem
{
    /// <summary>
    /// 影片管理类 
    /// </summary>
    public class MovieManage
    {
        /// <summary>
        /// 存储影片信息
        /// </summary>
        public static Dictionary<string, Movie> dirMovie = new Dictionary<string, Movie>();
        /// <summary>
        /// 存储放映厅
        /// </summary>
        public static Dictionary<string, Hall> dirHall = new Dictionary<string, Hall>();

        /// <summary>
        /// 初始化影片
        /// </summary>
        public static void InitialMovie()
        {
            InitialHall();
            //Movie m1 = new Movie("10003", "羞羞的铁拳", "Never Say Die", "喜剧片", "中国", "120分钟", 38, new DateTime(2017, 9, 29), "中国大陆上映", "image//yingpian//yp03.png", "image//yingpian//sp02.png");
            //dirHall["001"].ShowTime = "09:30";
            //dirHall["001"].ShowData = new DateTime(2017, 10, 31);
            //dirHall["001"].Price = m1.MoviePrice;
            //m1.HallInfo.Add(dirHall["001"]);
            //m1.HallInfo.Add(dirHall["003"]);
           
            //创建文件流
            FileStream fs = new FileStream("data//MovieInfo.bin", FileMode.Open);
            //创建序列化文本类
            BinaryFormatter bf = new BinaryFormatter();
            dirMovie = (Dictionary<string, Movie>)bf.Deserialize(fs);
            fs.Close();

        }
        /// <summary>
        /// 初始化放映厅
        /// </summary>
        public static void InitialHall()
        {
            //Seat[,] s1 = new Seat[13, 9];
            //Seat[,] s2 = new Seat[12, 7];
            //Seat[,] s3 = new Seat[13, 8];
            //Seat[,] s4 = new Seat[13, 6];
            //Seat[,] s5 = new Seat[8, 9];

            //Hall h1 = new Hall("001", "A区 1号厅", "3D", s1);
            //Hall h2 = new Hall("002", "A区 2号厅", "普通", s2);
            //Hall h3 = new Hall("003", "A区 3号厅", "3D", s3);
            //Hall h4 = new Hall("004", "B区 1号厅", "普通", s4);
            //Hall h5 = new Hall("005", "B区 2号厅", "3D", s5);
            //Hall h6 = new Hall("006", "C区 1号厅", "普通", s2);
            //Hall h7 = new Hall("007", "C区 IMAX厅", "3D", s1);
            //dirHall.Add(h1.H_ID, h1);
            //dirHall.Add(h2.H_ID, h2);
            //dirHall.Add(h3.H_ID, h3);
            //dirHall.Add(h4.H_ID, h4);
            //dirHall.Add(h5.H_ID, h5);
            //dirHall.Add(h6.H_ID, h6);
            //dirHall.Add(h7.H_ID, h7);

            //创建文件流
            FileStream fs = new FileStream("data//HallInfo.bin", FileMode.Open);
            //创建序列化文本类
            BinaryFormatter bf = new BinaryFormatter();
            dirHall = (Dictionary<string, Hall>)bf.Deserialize(fs);
            fs.Close();
        }

        #region 序列化数据

        /// <summary>
        /// 序列化影厅
        /// </summary>
        private static void SerializeHall()
        {
            //创建文件流
            FileStream fs = new FileStream("data//HallInfo.bin", FileMode.Create);
            //创建序列化文本类
            BinaryFormatter bf = new BinaryFormatter();
            //序列化
            bf.Serialize(fs, dirHall);
            fs.Close();
        }

        /// <summary>
        /// 序列化影片数据
        /// </summary>
        public static void SerializeMovie()
        {
            SerializeHall();
            //创建文件流
            FileStream fs = new FileStream("data//MovieInfo.bin", FileMode.Create);
            //创建序列化文本类
            BinaryFormatter bf = new BinaryFormatter();
            //序列化
            bf.Serialize(fs, dirMovie);
            fs.Close();
        }
        #endregion
    }
}
